package com.cs.contract.controller;

import com.cs.contract.service.CsDocumentsService;
import com.cs.contract.domain.CsDocuments;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author：zrx
 * @Package：com.cs.controller
 * @Project：RuoYi-Cloud
 * @name：CsDocumentsController
 * @Date：2024/11/27 16:54
 * @Filename：CsDocumentsController
 */
@RestController
@RequestMapping("/documents")
public class CsDocumentsController extends BaseController {
    private final CsDocumentsService csDocumentsService;
    private final RemoteFileService remoteFileService;

    public CsDocumentsController(CsDocumentsService csDocumentsService, RemoteFileService remoteFileService) {
        this.csDocumentsService = csDocumentsService;
        this.remoteFileService = remoteFileService;
    }

    @PostMapping("/upload1")
    public AjaxResult upload1(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            System.out.println("file:" + file);
            LoginUser loginUser = SecurityUtils.getLoginUser();
            R<SysFile> fileResult = remoteFileService.upload(file);
            System.out.println("fileResult:" + fileResult.getData().getName() + "\t" + fileResult.getData().getUrl());
            System.out.println("文件上传成功！。。。。");
            // 结果非空
            if (!StringUtils.isNull(fileResult) || !StringUtils.isNull(fileResult.getData())) {
                return AjaxResult.success("上传文件成功！", fileResult);
            }
        }
        return AjaxResult.error("上传文件异常，请联系管理员");
    }

    /**
     * 新增招标文件
     */
    @PostMapping
    public AjaxResult add(@RequestBody CsDocuments csDocuments) {
        return toAjax(csDocumentsService.insertCsDocuments(csDocuments));
    }
}
